package bgu.eliav;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Class3 extends Activity {
	static final int DIALOG_EXIT = 0;
	static final int DIALOG_ABOUT = 1;
	static final int DIALOG_SETTING = 2;

	private LinearLayout myScreen;
	final Activity class3 = this;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);
		myScreen = (LinearLayout) findViewById(R.id.LinearLayout01);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		MenuItem m1 = menu.findItem(R.id.backColor);
		SubMenu sM = m1.getSubMenu();
		sM.findItem(R.id.black).setCheckable(true);
		sM.findItem(R.id.black).setChecked(true);
		return true;

	}

	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.add:
			Intent intent = new Intent(this, NewUserUI.class);
			startActivity(intent);
			return true;

		case R.id.list:
			Intent intent1 = new Intent(this, ItemsList.class);
			startActivity(intent1);
			return true;

		case R.id.black:
			myScreen = (LinearLayout) findViewById(R.id.myScreen);
			myScreen.setBackgroundColor(Color.BLACK);
			item.setCheckable(true);
			item.setChecked(true);
			return true;

		case R.id.blue:
			myScreen = (LinearLayout) findViewById(R.id.myScreen);
			myScreen.setBackgroundColor(Color.BLUE);
			item.setCheckable(true);
			item.setChecked(true);
			return true;

		case R.id.red:
			myScreen = (LinearLayout) findViewById(R.id.myScreen);
			myScreen.setBackgroundColor(Color.RED);
			item.setCheckable(true);
			item.setChecked(true);

			return true;

		case R.id.green:
			myScreen = (LinearLayout) findViewById(R.id.myScreen);
			myScreen.setBackgroundColor(Color.GREEN);
			item.setCheckable(true);
			item.setChecked(true);
			return true;

		case R.id.about:
			showDialog(DIALOG_ABOUT);
		case R.id.exit:
			showDialog(DIALOG_EXIT);

		case R.id.local:
			item.setCheckable(true);
			item.setChecked(true);
			SharedPreferences ref = getSharedPreferences("bgu.eliav",
					MODE_PRIVATE);
			SharedPreferences.Editor ed = ref.edit();
			ed.putString("settings", "local");
			ed.commit();
			return true;

		case R.id.web:
			item.setCheckable(true);
			item.setChecked(true);
			SharedPreferences ref1 = getSharedPreferences("bgu.eliav",
					MODE_PRIVATE);
			SharedPreferences.Editor ed1 = ref1.edit();
			ed1.putString("settings", "web");
			ed1.commit();
			return true;

		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case DIALOG_ABOUT:
			return createDialogAbout("Ohad Levy  039465430\n "
					+ "Yair asraf  066137563\n");
		case DIALOG_EXIT:
			return creatingExitDialog();

		}

		return dialog;
	}

	private Dialog createDialogAbout(String msg) {
		Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.custom_dialog);
		dialog.setTitle("About the program: \n");
		TextView text = (TextView) dialog.findViewById(R.id.text);
		text.setText(msg);
		ImageView image = (ImageView) dialog.findViewById(R.id.image);
		image.setImageResource(R.drawable.icon);
		return dialog;

	}

	private Dialog creatingExitDialog() {
		final Activity localActivity = this;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to exit?");
		builder.setCancelable(false);

		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				localActivity.finish();
			}
		});

		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		return alert;

	}

	public void onResume() {
		super.onResume();
		SharedPreferences ref = getSharedPreferences("bgu.eliav", MODE_PRIVATE);
		String check = ref.getString("action", "");
		if (check.equals("exit")) {
			SharedPreferences.Editor ed = ref.edit();
			ed.putString("action", "continue");
			ed.commit();
			finish();
		}

		Button NewUserUIButton = (Button) this
				.findViewById(R.id.NewUserUIButton);
		NewUserUIButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(class3, NewUserUI.class);
				startActivity(intent);
			}
		});

		Button ItemListUIButton = (Button) this
				.findViewById(R.id.ItemListUIButton);
		ItemListUIButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(class3, ItemsList.class);
				startActivity(intent);
			}
		});

		Button SearchUIButton = (Button) this.findViewById(R.id.searchUIButton);
		SearchUIButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(class3, SearchUser.class);
				startActivity(intent);
			}
		});

	}

}
